package com.smartstate.dataManagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.Role;
import com.smartstate.dataManagement.service.RoleService;
import com.smartstate.dataManagement.util.DateTimeUtil;
import com.smartstate.dataManagement.util.StringUtils;
import com.smartstate.dataManagement.util.UserUtils;
import com.smartstate.dataManagement.vo.ResultCode;
import com.smartstate.dataManagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = {"角色控制器"})
@Slf4j
@RequestMapping("/Role")
public class RoleController {

    @Resource
    private RoleService service;

//    @ApiOperation(value = "新增角色")
//    @PostMapping("/saveRole")
//    public ResultVO<Boolean> saveOrUpdate(@RequestBody Role  role) {
//        return ResultVO.ok(service.add(role));
//    }

    @ApiOperation(value = "新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称",paramType = "query",required = true),
            @ApiImplicitParam(name = "note", value = "备注",paramType = "query",required = false),
    })
    @PostMapping("/addRole")
    @Transactional
    public ResultVO<String> addRole(@RequestParam String roleName,@RequestParam(required = false) String note,@RequestBody(required = false) List<String> ids) {
            return service.addRole(roleName,note,ids);
    }



    @PutMapping("/updateById")
    @ApiOperation(value = "根据id修改角色")
    public ResultVO<ResultCode> updateById(@RequestBody Role item) {
        return  service.updateByRoleID(item);
    }

    @PutMapping("/updatePermById")
    @ApiOperation(value = "根据id修改角色对应的权限")
    public ResultVO<String> updatePermById(@RequestParam String roleId,@RequestParam(required = false) String roleName,@RequestParam(required = false) String note,@RequestBody(required = false) List<String> ids) {
        return service.updatePermById(roleId,roleName,note,ids);
    }


    @DeleteMapping ("/removeByIds")
    @ApiOperation(value = "根据ID批量删除角色")
    public ResultVO<Boolean> removeByIds(@RequestBody List<Integer> item) {
        if(null !=item) {
            return ResultVO.ok(service.removeByIds(item));
        }
        return  ResultVO.fail("参数缺失， 请输入参数");
    }



    @DeleteMapping ("/delRoleByID")
    @ApiOperation(value = "根据ID删除单个角色")
    @Transactional
    public  ResultVO<ResultCode> delRoleByID(@RequestParam String id) {
        if(StringUtils.isNotBlank(id)){
            return service.delRoleByID(id);
        }
        return  ResultVO.fail("参数缺失，请输入角色ID");
    }


    @DeleteMapping ("/delBatchRoleByID")
    @ApiOperation(value = "根据角色ID集合批量删除角色")
    @Transactional
    public  ResultVO<Boolean> delBatchRoleByID(@RequestBody List<String> ids) {
        if(ids.size()>0){
            return ResultVO.ok(service.delBatchRoleByID(ids));
        }
        return  ResultVO.fail("参数缺失，请输入角色ID");
    }

    @PostMapping ("/getRoleBySpec")
    @ApiOperation(value = "单表分页查询角色")
    public ResultVO<Page<Role>> getRoleBySpec(@RequestBody PageDTO<Role> pageDTO) {
        if(null!= pageDTO){
            return ResultVO.ok(service.queryAllPageByDTO(pageDTO));
        }
        return  ResultVO.fail("参数缺失， 请输入参数");

    }

    @GetMapping ("/getRoleByName")
    @ApiOperation(value = "根据角色名查询角色")
    public ResultVO<Role> getRoleBySpec(@RequestParam String roleName) {
        if(StringUtils.isNotBlank(roleName)) {
            return ResultVO.ok(service.getRoleByName(roleName));
        }
        return  ResultVO.fail("参数缺失， 请输入角色名称");
    }


    @GetMapping ("/getRoleById")
    @ApiOperation(value = "根据角色id查询角色")
    public ResultVO<Role> getRoleById(@RequestParam String id) {
        if(StringUtils.isNotBlank(id)){
            return ResultVO.ok(service.getByRoleId(id));
        }
        return  ResultVO.fail("参数缺失，请输入参数");

    }

    @GetMapping ("/getEnableAll")
    @ApiOperation(value = "查询所有有效的角色")
    public ResultVO<List<Role>> getAllRole() {
        return  ResultVO.ok(service.getEnableAll());
    }

//
//    @GetMapping ("/getRoleByNameOrStatus")
//    @ApiOperation(value = "根据角色名和角色状态查询角色 ")
//    public Page<Role> getRoleByNameAndStatus(@RequestBody PageDTO<Role> pageDTO) {
//        return  service.getRoleByNameAndStatus(pageDTO);
//    }


    @PostMapping ("/getRoleByName")
    @ApiOperation(value = "角色按照姓名模糊查询")
    public ResultVO<List<Role>> getRoleByName(@RequestParam  String  roleName) {
        return  ResultVO.ok(service.getRoleLikeName(roleName));
    }

}
